package www.ym.com.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.entity.Category;
import www.ym.com.entity.Note;
import www.ym.com.service.NoteService;

import java.util.List;

@RestController
@RequestMapping("/note")
@Slf4j

public class NoteController {

    @Autowired
    private NoteService noteService;

    /**
     * 新增
     *
     * @param note
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Note note) {
        noteService.save(note);
        return R.success(null, "添加成功");
    }


    /**
     * 修改
     *
     * @param note
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody Note note) {
        noteService.updateById(note);
        return R.success(null, "修改成功");
    }

    /**
     * 查询
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/search")
    public RL<Note> search(@RequestBody JSONObject jsonObject) {
        Page<Note> userPage = noteService.pageNote(jsonObject);
        List<Note> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = userPage.getCurrent();
        long pageSize = userPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }


    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/del")
    public R<String> delete(Long id) {
        LambdaQueryWrapper<Note> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Note::getId, id);
        noteService.remove(queryWrapper);
        return R.success(null, "删除成功");
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    @PostMapping("/details")
    public R<Note> details(@RequestBody Note note) {
        log.info("id" + note);
//        LambdaQueryWrapper<Note> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Note::getId,note.getId());
        Note note1 = noteService.getById(note.getId());
        return R.success(note1, "null");
    }

    @PostMapping("/list")
    public RL<Note> list(@RequestBody JSONObject jsonObject) {
        Page<Note> userPage = noteService.pageNote(jsonObject);
        List<Note> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = userPage.getCurrent();
        long pageSize = userPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }

}


